﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TailMove : MonoBehaviour
{
    public float speed = 2f;

    [HideInInspector]
    public Vector3 endPos;

    private void OnEnable()
    {
        MoveController.Move += Move;
    }
    private void OnDisable()
    {
        MoveController.Move -= Move;
    }
    void Update()
    {
        // 这里可以用DoTween动画插件，这样就可以用时间控制，时间为刷新坐标的时间间隔
        if (this.transform.position == endPos) return;

        //endPos.x
        this.transform.position = Vector3.MoveTowards(this.transform.position, endPos, speed * Time.deltaTime);
    }

    private void Move()
    {
        float date = transform.transform.localScale.x;

        endPos.x += Input.GetAxis("Mouse X") * ((1-date)*0.2f);
    }

    public void Init(Vector3 _pos)
    {
        endPos = this.transform.position = _pos;
    }

    public void SetEndPos(Vector3 _pos)
    {
        endPos = _pos;
    }
}
